const { Guestbook } = require('../../../model/guestbook');
const { User } = require('../../../model/user');

const pagination = require('mongoose-sex-page')

module.exports = async (req, res) => {
  const { pageIndex, pageSize, content = '', nickname = null} = req.query
  let query = {
	content: {$regex: content, $options:'i'}
  }
  if (nickname) {
	  const currenUser = await User.findOne({
		nickname
	  })
	  if (!currenUser) {
		  res.status(403)
		  res.json({
		      message: '没有搜到相关数据！'
		  })
		  return
	  }
	  query.user = currenUser._id
  }
  const guestbooks = await pagination(Guestbook).find(query).sort({_id: -1}).page(pageIndex).size(pageSize).populate('user', 'username nickname avatar').exec()
  res.json({
      data: guestbooks
  })
}